//
//  JGForgetPwdContentView.m
//  JGRider
//
//  Created by 单启志 on 2018/3/2.
//  Copyright © 2018年 ffq. All rights reserved.
//

#import "JGForgetPwdContentView.h"
#import "FQCountDownButton.h"
#import "FQGetValidateCodeHelper.h"
@interface JGForgetPwdContentView()
@property (weak, nonatomic) IBOutlet UITextField *accountTF;//账号
@property (weak, nonatomic) IBOutlet UITextField *validateCodeTF;//验证码
@property (weak, nonatomic) IBOutlet UITextField *pwdNewTF;//新密码


@property (weak, nonatomic) IBOutlet FQCountDownButton *validateCodeBtn;//验证码btn
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;//确定

@property(nonatomic,copy)NSString *realValidateCode;
@end
@implementation JGForgetPwdContentView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self=[[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil]lastObject];
        self.frame=frame;
        [self layoutSubViews];
    }
    return self;
}

-(void)awakeFromNib{
    [super awakeFromNib];
}

-(void)layoutSubViews{
    self.confirmBtn.layer.cornerRadius=AdaptedWidth(3);
    self.confirmBtn.layer.masksToBounds=YES;
    self.validateCodeBtn.layer.cornerRadius=AdaptedWidth(5);
    self.validateCodeBtn.layer.masksToBounds=YES;
    self.validateCodeBtn.layer.borderColor=HEXCOLOR(0xfea129).CGColor;
    self.validateCodeBtn.layer.borderWidth=AdaptedHeight(0.5);
    [self.validateCodeBtn setTitleColor:HEXCOLOR(0xfea129) forState:UIControlStateNormal];
    [self.validateCodeBtn setTitleColor:HEXCOLOR(0xffffff) forState:UIControlStateDisabled];
}

//获取验证码
- (IBAction)onValidateCodeBtnClick:(FQCountDownButton*)sender {
    FQWeakSelf(self);
    [[FQGetValidateCodeHelper share]getValidateCodeWithMobile:self.accountTF.text  view:self button:sender sucess:^(NSString *code) {
        weakself.realValidateCode=code;
    } faile:nil];

    if ([self.delegate respondsToSelector:@selector(contentViewValidateCodeEvent)]) {
        [self.delegate contentViewValidateCodeEvent];
    }
    
}

//确定
- (IBAction)onConfirmBtnClick:(id)sender {

    if ([self validateInput]==NO) {
        return;
    }

    if ([self.delegate respondsToSelector:@selector(contentViewConfirmEventWithParams:)]) {
        NSMutableDictionary *dic=[NSMutableDictionary dictionary];
        dic[@"mobile"]=[self.accountTF.text stringByTrim];
        dic[@"code"]=[self.validateCodeTF.text stringByTrim];
        dic[@"pwd"]=[self.pwdNewTF.text stringByTrim];
        dic[@"roleId"]=roleId;
        [self.delegate contentViewConfirmEventWithParams:dic];
    }
}
#pragma mark - 验证输入框合法性
//验证输入内容
-(BOOL)validateInput{

    if ([self validateWithFT:self.accountTF withMessage:@"请输入手机号码"]==NO) {
        return NO;
    }
    if ([self validateWithFT:self.validateCodeTF withMessage:@"请输入验证码"]==NO) {
        return NO;
    }
    if ([self validateCode]==NO) {
        return NO;
    }
    if ([self validateWithFT:self.pwdNewTF withMessage:@"请输入密码"]==NO) {
        return NO;
    }
    if ([self validatePwdFormat]==NO) {
        return NO;
    }
    return YES;

}

//验证输入框非空
-(BOOL)validateWithFT:(UITextField *)tf withMessage:(NSString*)message{
    if (![tf.text.stringByTrim isEqualToString:@""]&&[tf.text isNotBlank]) {
        return YES;
    }else{
        [MBProgressHUD showError:message ToView:self];
        return NO;
    }
}

-(BOOL)validateCode{
    if (FQIsEmpty(self.realValidateCode)) {
        [MBProgressHUD showError:@"请先获取验证码" ToView:self];
        return NO;
    }
    if ([[self.validateCodeTF.text stringByTrim] isEqualToString:self.realValidateCode]) {
        return YES;
    }else{
        [MBProgressHUD showError:@"验证码错误" ToView:self];
        return NO;
    }
}
-(BOOL)validatePwdFormat{
    if (self.pwdNewTF.text.length>=6&&self.pwdNewTF.text.length<=12) {
        return YES;
    }else{
        [MBProgressHUD showError:@"密码长度必须为6-12位" ToView:self];
        return NO;
    }
}
@end
